home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Window Info / STARTUP.TXT < prev    next >
Encoding:
Text File  |  2000-05-25  |  5.4 KB  |  139 lines

  1.  
  2. Windows Application Startup
  3.  
  4. This topic describes the startup requirements of applications 
  5. for the Windows operating system. It also discusses the steps 
  6. needed to initialize an application before its entry-point 
  7. function, WinMain, can be called. 
  8.  
  9. Startup Requirements
  10.  
  11. When Windows starts an application, it calls a startup 
  12. routine supplied with the application rather than the 
  13. application's WinMain function. The startup routine is 
  14. responsible for initializing the application, calling WinMain
  15. , and exiting the application when WinMain returns control. 
  16. When Windows first calls the startup routine, the processor 
  17. registers have the following values: 
  18.  
  19. Register Value
  20.  
  21. AX       Contains zero. 
  22. BX       Specifies the size, in bytes, of the stack. 
  23. CX       Specifies the size, in bytes, of the heap. 
  24. DI       Contains a handle identifying the new application 
  25.          instance. 
  26. SI       Contains a handle identifying the previous 
  27.          application instance. 
  28. BP       Contains zero. 
  29. ES       Contains the segment address of the program segment 
  30.          prefix (PSP). 
  31. DS       Contains the segment address of the automatic data 
  32.          segment for the application. 
  33. SS       Same as the DS register. 
  34. SP       Contains the offset to the first byte of the 
  35.          application stack. 
  36. To initialize and exit a Windows application, the startup 
  37. routine must follow these steps: 
  38. 1 Initialize the task by using the InitTask function. InitTask
  39.    also returns values that the startup routine passes to the 
  40.    WinMain function. 
  41. 2 Clear the event that started the task by calling the 
  42.   WaitEvent function. 
  43. 3 Initialize the queue and support routines for the 
  44.   application by calling the InitApp function with the 
  45.   instance handle returned by the InitTask function. 
  46. 4 Call the entry point for the application, the WinMain
  47.   function. 
  48. 5 Exit the application by calling the MS-DOS End Program 
  49.   function (Interrupt 21h Function 4Ch) when WinMain returns. 
  50. Although the startup routine is essentially the same for all 
  51. Windows applications, a variety of startup routines may need 
  52. to be developed to accommodate the different memory models and 
  53. high-level language run-time libraries used by Windows 
  54. applications. If a Windows application uses functions and 
  55. variables provided by run-time libraries, the startup routine 
  56. may need to be customized to initialize the library at the 
  57. same time as the application. Customizing the startup routine 
  58. for run-time library initialization is entirely dependent on 
  59. the library and is, therefore, beyond the scope of this topic. 
  60.  
  61. Example of a Startup Routine
  62.  
  63. A startup routine initializes and exits a Windows 
  64. application. The routine in the following example, the 
  65. __astart function, shows the code needed for startup, which 
  66. includes Cmacros defined in the CMACROS.INC header file. When 
  67. assembled, this code is suitable for small-model Windows 
  68. applications that do not use run-time libraries: 
  69. .xlist
  70. memS = 1    ; small memory model
  71. ?DF = 1     ; Do not generate default segment definitions.
  72. ?PLM = 1;
  73. ?WIN = 1;
  74. include cmacros.inc
  75. .list
  76. STACKSLOP = 256
  77. createSeg   _TEXT,CODE,PARA,PUBLIC,CODE
  78. createSeg NULL, NULL, PARA,PUBLIC,BEGDATA,DGROUP
  79. createSeg _DATA,DATA, PARA,PUBLIC,DATA,   DGROUP
  80. defGrp      DGROUP,DATA
  81. assumes DS,DATA
  82. sBegin      NULL
  83.             DD  0
  84. labelW      <PUBLIC,rsrvptrs>
  85. maxRsrvPtrs = 5
  86.             DW  maxRsrvPtrs
  87.             DW  maxRsrvPtrs DUP (0)
  88. sEnd        NULL
  89. sBegin  DATA
  90. staticW hPrev,0             ; Save WinMain parameters.
  91. staticW hInstance,0
  92. staticD lpszCmdline,0
  93. staticW cmdShow,0
  94. sEnd    DATA
  95. externFP   <INITTASK>
  96. externFP   <WAITEVENT>
  97. externFP   <INITAPP>
  98. externFP   <DOS3CALL>
  99. externP    <WINMAIN>
  100. sBegin  CODE
  101. assumes CS,CODE
  102. labelNP <PUBLIC,__astart>
  103.         xor     bp,bp                   ; zero bp
  104.         push    bp
  105.         cCall   INITTASK                ; Initialize the task.
  106.         or      ax,ax
  107.         jz      noinit
  108.         add     cx,STACKSLOP            ; Add in stack slop space.
  109.         jc      noinit                  ; If overflow, return error.
  110.         mov     hPrev,si
  111.         mov     hInstance,di
  112.         mov     word ptr lpszCmdline,bx
  113.         mov     word ptr lpszCmdline+2,es
  114.         mov     cmdShow,dx
  115.         xor     ax,ax                   ; Clear initial event that
  116.         cCall   WAITEVENT,<ax>          ;   started this task.
  117.         cCall   INITAPP,<hInstance>     ; Initialize the queue.
  118.         or      ax,ax
  119.         jz      noinit
  120.         cCall   WINMAIN,<hInstance,hPrev,lpszCmdline,cmdShow>
  121. ix:
  122.         mov     ah,4Ch
  123.         cCall   DOS3CALL                ; Exit with return code from app.
  124. noinit:
  125.         mov     al,0FFh                 ; Exit with error code.
  126.         jmp short ix
  127. sEnd     CODE
  128.         end __astart                    ; start address
  129. Windows requires the null segment (containing the rsrvptrs 
  130. array), which is defined at the beginning of this sample. The 
  131. InitTask function copies the top, minimum, and bottom address 
  132. offsets of the stack into the third, fourth, and fifth 
  133. elements of the rsrvptrs array. Applications can use these 
  134. offsets to check the amount of space available on the stack. 
  135. The debugging version of Windows also uses these offsets to 
  136. check the stack. Applications must, therefore, not change 
  137. these offsets, since doing so can cause a system debugging 
  138. error (RIP). 
  139.